package team.umbc;


import com.facebook.android.*;  
import com.facebook.android.Facebook.*;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class FirstTime extends Activity implements OnClickListener{
	
	Facebook facebook;
	View continueBtn;
	private SharedPreferences settings;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first_time);
		
		View facebookBtn = findViewById(R.id.facebook_connect);
		facebookBtn.setOnClickListener(this);
		View foursquareBtn = findViewById(R.id.foursquare_connect);
		foursquareBtn.setOnClickListener(this);
		continueBtn = findViewById(R.id.continue_button);
		continueBtn.setEnabled(false);
		continueBtn.setOnClickListener(this);
		View backBtn = findViewById(R.id.back);
		backBtn.setOnClickListener(this);
		
		facebook = new Facebook(CheckinActivity.APP_ID);
		settings = getSharedPreferences(CheckinActivity.PREFS_NAME, 0);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.facebook_connect:
			facebook();
			break;
		case R.id.foursquare_connect:
			
			break;
		case R.id.continue_button:
			finish();
			break;
		case R.id.back:
			onDestroy();
			break;
		}
		if(facebook.isSessionValid()){
			continueBtn.setEnabled(true);
		}
	}

	boolean isLogged = false;
	
	private void facebook() {
		
	    
		facebook.authorize(this, new String[] {"publish_checkins", "read_friendlists", "offline_access"} , new DialogListener() {
            @Override
            public void onComplete(Bundle values) { 
            	SharedPreferences.Editor editor = settings.edit();
            	editor.putString(CheckinActivity.ACCESS_TOKEN, facebook.getAccessToken());
            	editor.putLong(CheckinActivity.EXPIRES_IN, facebook.getAccessExpires());
            	editor.commit();
            	continueBtn.setEnabled(true);
            }
            
			@Override
            public void onFacebookError(FacebookError error) {
			}

            @Override
            public void onError(DialogError e) {
            }

            @Override 
            public void onCancel() {}
        });
		
	}
	public void setLoggedStatusIn(boolean truth){
		if (truth){
			Intent i = new Intent(this, CheckinActivity.class);
			i.putExtra("facebook", facebook);
		} else {
			facebook = null;
			Intent i = new Intent(this, CheckinActivity.class);
			i.putExtra("facebook", facebook);
		}
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        facebook.authorizeCallback(requestCode, resultCode, data);
    }

}
